<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Client.class.php";

class Log
{
	static private $client;
	
	private $file_name;
	private $file_path;
	private $lines = null;
	
	function Log($file)
	{
		if ( empty(self::$client) )
		{
			self::$client = Client::GetInstance();
		}
	
		$this->file_name = $file;
		$this->file_path = dirname(__FILE__)."/../logs/".$file.".log";
	}
	
	function AddError($e)
	{
		$this->lines .= date("Y-m-d\tH:i:s")."\t".$e."\n\t";
		$this->lines .= $_SERVER['PHP_SELF']."\n\t";
		
		$this->lines .= self::$client->GetIp();
		
		if (self::$client->GetForwardedIp() != null)
		{
			$this->lines .= "\t".self::$client->GetForwardedIp();
		}
		
		$this->lines .= "\n";
	}
	
	function AddMessage($e)
	{
		$this->AddError($e);
	}
	
	function __destruct()
	{
		if ($this->lines != null)
		{
			if (file_put_contents( $this->file_path, $this->lines, FILE_APPEND ) == false)
			{
				trigger_error("Could not append lines to log file: ".$this->file_name.".log", E_USER_ERROR);
			}
		}
	}
}

?>